<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Plugins\QrCode;

class QrMask
{
    public array $runLength = [];

    public function __construct()
    {
        $this->runLength = array_fill(0, 178, 0);
    }

    public function writeFormatInformation($width, &$frame, $mask, $level): int
    {
        $blacks = 0;
        $format = QrSpec::getFormatInfo($mask, $level);
        for ($i = 0; $i < 8; $i++) {
            if ($format & 1) {
                $blacks += 2;
                $v = 0x85;
            } else {
                $v = 0x84;
            }
            $frame[8][$width - 1 - $i] = chr($v);
            if ($i < 6) {
                $frame[$i][8] = chr($v);
            } else {
                $frame[$i + 1][8] = chr($v);
            }
            $format = $format >> 1;
        }
        for ($i = 0; $i < 7; $i++) {
            if ($format & 1) {
                $blacks += 2;
                $v = 0x85;
            } else {
                $v = 0x84;
            }
            $frame[$width - 7 + $i][8] = chr($v);
            if ($i == 0) {
                $frame[8][7] = chr($v);
            } else {
                $frame[8][6 - $i] = chr($v);
            }
            $format = $format >> 1;
        }
        return $blacks;
    }

    public function mask0($x, $y): int
    {
        return ($x + $y) & 1;
    }

    public function mask1($x, $y): int
    {
        unset($x);
        return ($y & 1);
    }

    public function mask2($x, $y): int
    {
        unset($y);
        return ($x % 3);
    }

    public function mask3($x, $y): int
    {
        return ($x + $y) % 3;
    }

    public function mask4($x, $y): int
    {
        return (intval($y / 2) + intval($x / 3)) & 1;
    }

    public function mask5($x, $y): int
    {
        return (($x * $y) & 1) + ($x * $y) % 3;
    }

    public function mask6($x, $y): int
    {
        return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
    }

    public function mask7($x, $y): int
    {
        return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
    }

    private function generateMaskNo($maskNo, $width, $frame): array
    {
        $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
        for ($y = 0; $y < $width; $y++) {
            for ($x = 0; $x < $width; $x++) {
                if (ord($frame[$y][$x]) & 0x80) {
                    $bitMask[$y][$x] = 0;
                } else {
                    $maskFunc = 0;
                    switch ($maskNo) {
                        case 0:
                        {
                            $maskFunc = $this->mask0($x, $y);
                            break;
                        }
                        case 1:
                        {
                            $maskFunc = $this->mask1($x, $y);
                            break;
                        }
                        case 2:
                        {
                            $maskFunc = $this->mask2($x, $y);
                            break;
                        }
                        case 3:
                        {
                            $maskFunc = $this->mask3($x, $y);
                            break;
                        }
                        case 4:
                        {
                            $maskFunc = $this->mask4($x, $y);
                            break;
                        }
                        case 5:
                        {
                            $maskFunc = $this->mask5($x, $y);
                            break;
                        }
                        case 6:
                        {
                            $maskFunc = $this->mask6($x, $y);
                            break;
                        }
                        case 7:
                        {
                            $maskFunc = $this->mask7($x, $y);
                            break;
                        }
                    }
                    $bitMask[$y][$x] = ($maskFunc == 0) ? 1 : 0;
                }
            }
        }
        return $bitMask;
    }

    public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false): int
    {
        $b = 0;
        $bitMask = $this->generateMaskNo($maskNo, $width, $s);
        if ($maskGenOnly) return 0;
        $d = $s;
        for ($y = 0; $y < $width; $y++) {
            for ($x = 0; $x < $width; $x++) {
                if ($bitMask[$y][$x] == 1) {
                    $d[$y][$x] = chr(ord($s[$y][$x]) ^ intval($bitMask[$y][$x]));
                }
                $b += ord($d[$y][$x]) & 1;
            }
        }
        return $b;
    }

    public function makeMask($width, $frame, $maskNo, $level): array
    {
        $masked = array_fill(0, $width, str_repeat("\0", $width));
        $this->makeMaskNo($maskNo, $width, $frame, $masked);
        $this->writeFormatInformation($width, $masked, $maskNo, $level);
        return $masked;
    }

    public function calcN1N3($length)
    {
        $demerit = 0;
        for ($i = 0; $i < $length; $i++) {
            if ($this->runLength[$i] >= 5) {
                $demerit += (QR_N1 + ($this->runLength[$i] - 5));
            }
            if ($i & 1) {
                if (($i >= 3) && ($i < ($length - 2)) && ($this->runLength[$i] % 3 == 0)) {
                    $fact = intval($this->runLength[$i] / 3);
                    if (($this->runLength[$i - 2] == $fact) &&
                        ($this->runLength[$i - 1] == $fact) &&
                        ($this->runLength[$i + 1] == $fact) &&
                        ($this->runLength[$i + 2] == $fact)) {
                        if (($this->runLength[$i - 3] < 0) || ($this->runLength[$i - 3] >= (4 * $fact))) {
                            $demerit += QR_N3;
                        } else if ((($i + 3) >= $length) || ($this->runLength[$i + 3] >= (4 * $fact))) {
                            $demerit += QR_N3;
                        }
                    }
                }
            }
        }
        return $demerit;
    }

    public function evaluateSymbol($width, $frame)
    {
        $demerit = 0;
        $frameYM = [];
        for ($y = 0; $y < $width; $y++) {
            $head = 0;
            $this->runLength[0] = 1;
            $frameY = $frame[$y];
            if ($y > 0) $frameYM = $frame[$y - 1];
            for ($x = 0; $x < $width; $x++) {
                if (($x > 0) && ($y > 0)) {
                    $b22 = ord($frameY[$x]) & ord($frameY[$x - 1]) & ord($frameYM[$x]) & ord($frameYM[$x - 1]);
                    $w22 = ord($frameY[$x]) | ord($frameY[$x - 1]) | ord($frameYM[$x]) | ord($frameYM[$x - 1]);
                    if (($b22 | ($w22 ^ 1)) & 1) {
                        $demerit += QR_N2;
                    }
                }
                if (($x == 0) && (ord($frameY[$x]) & 1)) {
                    $this->runLength[0] = -1;
                    $head = 1;
                    $this->runLength[$head] = 1;
                } else if ($x > 0) {
                    if ((ord($frameY[$x]) ^ ord($frameY[$x - 1])) & 1) {
                        $head++;
                        $this->runLength[$head] = 1;
                    } else {
                        $this->runLength[$head]++;
                    }
                }
            }
            $demerit += $this->calcN1N3($head + 1);
        }
        for ($x = 0; $x < $width; $x++) {
            $head = 0;
            $this->runLength[0] = 1;
            for ($y = 0; $y < $width; $y++) {
                if ($y == 0 && (ord($frame[$y][$x]) & 1)) {
                    $this->runLength[0] = -1;
                    $head = 1;
                    $this->runLength[$head] = 1;
                } else if ($y > 0) {
                    if ((ord($frame[$y][$x]) ^ ord($frame[$y - 1][$x])) & 1) {
                        $head++;
                        $this->runLength[$head] = 1;
                    } else {
                        $this->runLength[$head]++;
                    }
                }
            }
            $demerit += $this->calcN1N3($head + 1);
        }
        return $demerit;
    }

    public function mask(int $width, array $frame, int $level): array
    {
        $minDemerit = PHP_INT_MAX;
        $checked_masks = [0, 1, 2, 3, 4, 5, 6, 7];
        $bestMask = $frame;
        foreach ($checked_masks as $i) {
            $mask = array_fill(0, $width, str_repeat("\0", $width));
            $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
            $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
            $blacks = intval(100 * $blacks / ($width * $width));
            $demerit = intval(intval(abs($blacks - 50) / 5) * QR_N4);
            $demerit += $this->evaluateSymbol($width, $mask);
            if ($demerit < $minDemerit) {
                $minDemerit = $demerit;
                $bestMask = $mask;
            }
        }
        return $bestMask;
    }
}